package com.wuwei.elearning.utils;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Component
public class TimeUtils {
    public Date dateFormat(String dateString){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public LocalTime timeFormat (String timeString){
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

        // 将字符串解析为 LocalTime 对象
        LocalTime localTime = LocalTime.parse(timeString, formatter);

        return localTime;    }

    /**
     * 获取当前日期的格式化字符串
     *
     * @param pattern 日期格式 例如："yyyy-MM/dd"
     * @return 格式化后的日期字符串
     */
    public static String getNowDateLongStr(String pattern) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return now.format(formatter);
    }

    /**
     * 传入时间戳，获取这个时间戳的那一天的第一秒
     * @return
     */
    public static LocalDateTime getStartLocalTime(Long timeStamp) {
        LocalDate date = Instant.ofEpochMilli(timeStamp).atZone(zone).toLocalDate();
        //获取当天的开始
        return date.atStartOfDay();
    }

    private static final ZoneId zone = ZoneId.systemDefault();

    /**
     * 传入时间戳，获取这个时间戳的那一天的最后
     * @return
     */
    public static LocalDateTime getEndLocalTime(Long timeStamp) {
        LocalDate date = Instant.ofEpochMilli(timeStamp).atZone(zone).toLocalDate();
        // 设置为一天的结束
        return date.atTime(LocalTime.MAX);
    }
}
